package com.yycx.module.bus.provider.handler.haiying.order.waybill;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ConvertUtils;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.hy.BusHyorderSendinfo;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.enums.HySaleTypeEnum;
import com.yycx.module.bus.provider.service.hy.BusHyorderSendinfoService;
import com.yycx.module.bus.provider.service.hy.HyOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * 已打印订单列表
 *
 * @author zyf
 */
@Component
public class HyWaybillTab3ListHandler implements PageInterceptor<HyOrder> {

    @Autowired
    private HyOrderDetailService orderDetailService;


    @Autowired
    private BusHyorderSendinfoService sendinfoService;

    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaQuery cq, PageParams pageParams, EntityMap params) {
        String beginDate = params.get("beginDate", DateUtils.formatDate());
        String endDate = params.get("endDate", DateUtils.formatDate());
        cq.eq(HyOrder.class, "companyId",OpenHelper.getCompanyId());
        cq.eq("orderState", 2);
        String orderNo=cq.getParams("orderNo");
        if(FlymeUtils.isNotEmpty(orderNo)) {
            cq.like("ho.outOrderNo", orderNo);
        }
        cq.ge("ho.sendDate",beginDate);
        cq.eq("ho.orderType", HySaleTypeEnum.CHA.getCode());
        cq.le("ho.sendDate",endDate);
        cq.eq("sendState", 1);
        cq.gt("outPrintTag", 0);
        cq.like(HyOrder.class,"customAddress");
        cq.orderByDesc("ho.orderNo");
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<HyOrder> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            String orderId = entityMap.get("orderId");
            BigDecimal drugJs = orderDetailService.countDrugJs(orderId);
            entityMap.put("drugJsCount", drugJs.setScale(0, BigDecimal.ROUND_UP));
            List<String> orderDetailsIds = orderDetailService.getOrderDetails(orderId);
            entityMap.put("orderDetailsIds", ConvertUtils.listToString(orderDetailsIds));
            BusHyorderSendinfo sendinfo=sendinfoService.findByOrderId(orderId);
            if(FlymeUtils.isNotEmpty(sendinfo)){
                entityMap.put("fastMailNo",sendinfo.getFastMailNo());
                entityMap.put("fastMailName",sendinfo.getFastMailName());
            }
        }
        return result;
    }

}
